// pages/wode/collect/collect.js
//index.js
//获取应用实例
const app = getApp();
var njzxw = require('../../../utils/njzxw.js');
var url = require('../../../utils/url.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    playSize: 0,
    playTitle: '',
    innerAudioContext: null,
    play: false,
    playNum: null,
    payCount: 0,
    url: 0,
    tab: [
      {
        name: '百科',
        url: '/pages/wode/collect/collect',
        active: false,
        fun: 'redirectTo'
      },
      // {
      //   name: '专家',
      //   url: '/pages/wode/collect/zhuanjia',
      //   active: false,
      //   fun: 'redirectTo'
      // },
      {
        name: '视频',
        url: '/pages/wode/collect/shipin',
        active: false,
        fun: 'redirectTo'
      },
      {
        name: '音频',
        url: '/pages/wode/collect/yinpin',
        active: true,
        fun: 'redirectTo'
      },
      {
        name: '论坛',
        url: '/pages/wode/collect/luntan',
        active: false,
        fun: 'redirectTo'
      }
    ]
  },
  showActivety: function (e) {
    var $this = this;
    var urls = njzxw.getDataVal(e, 'url');
    wx.showActionSheet({
      itemList: [ '取消收藏'],
      success: function (res) {
        if (res.tapIndex == 0) {
          $this.deleteCommon(e);
        }
      }
    })
  },
  /**
   * 取消收藏
   */
  deleteCommon: function (e) {
    var res_id = njzxw.getDataVal(e, 'id');
    var type = njzxw.getDataVal(e, 'stype');
    var $this = this;
    njzxw.alert({
      msg: '确定需要取消收藏吗？',
      confirm: true,

      fun: function (a) {

        if (a) {
          njzxw.ajax({
            url: url.encyclopediaInfoFront.deleteShouChang,
            data: {
              id: res_id,
              type: type
            },
            load: true,
            fun: function (a, b) {
              wx.hideLoading();
              if (a) {
                if (b.data.state == 1) {
                  njzxw.alert({
                    msg: '操作成功',
                    fun: function () {
                      njzxw.loadStart($this);
                      $this.loadData($this);
                    }
                  });

                } else {
                  njzxw.alert({
                    error: true,
                    msg: '删除失败'
                  });
                }
              } else {
                njzxw.alert({
                  error: true,
                  msg: '操作失败'
                });
              }
            }
          });
        }
      }
    });
  },
  pausePay: function () {
    this.data.innerAudioContext.pause();
    this.setData({
      play: false
    });
  },
  hidePay: function () {
    if (this.data.innerAudioContext != null) {
      this.data.innerAudioContext.destroy();
      this.data.list[this.data.playNum].play = false;
    }
    // console.log(this.data.playNum, this.data.list[this.data.playNum]);

    this.setData({
      innerAudioContext: null,
      play: false,
      list: this.data.list,
      playNum: null,
      playSize: 0,
      playTitle: ''
    });
  },
  changePay: function (event) {
    this.data.innerAudioContext.pause();
    var val = event.detail.value;
    console.log(val / 100);
    var num = (val / 100) * this.data.payCount;
    console.log(num + "-" + this.data.payCount);
    this.setData({
      playSize: val
    });

    this.data.innerAudioContext.seek(num);
    this.data.innerAudioContext.play();
  },
  /**
   * 恢复播放
   */
  hfPlay: function () {
    this.data.innerAudioContext.play();
    this.setData({
      play: true
    });
  },
  showPay: function (e) {
    var $this = this;
    this.hidePay();
    // console.log(this.data.list[e.currentTarget.dataset.num].play);
    var innerAudioContext = wx.createInnerAudioContext();
    this.data.list[e.currentTarget.dataset.num].play = true;
    this.setData({
      innerAudioContext: innerAudioContext,
      play: true,
      list: this.data.list,
      playNum: e.currentTarget.dataset.num,
      playTitle: this.data.list[e.currentTarget.dataset.num].res_name
    });
    // console.log(this.data.list[e.currentTarget.dataset.num].play);
    $this.data.innerAudioContext.autoplay = true;
    var src = this.data.list[e.currentTarget.dataset.num].res_path;
    if (src == undefined || src == '') {
      src = this.data.list[e.currentTarget.dataset.num].url;
    }

    $this.data.innerAudioContext.src = src;
    $this.data.innerAudioContext.onError((res) => {
      wx.showModal({
        title: '系统提示',
        content: '音频播放错误'
      })
      $this.setData({
        play: false
      });
    });
    this.data.innerAudioContext.onCanplay(function () {
      console.log(innerAudioContext.duration);
    });
    this.data.innerAudioContext.onEnded(function () {
      $this.setData({
        playSize: 0
      });
    });
    this.data.innerAudioContext.onTimeUpdate(function () {
      var jindu = parseInt((innerAudioContext.currentTime / innerAudioContext.duration) * 100);
      $this.setData({
        playSize: jindu,
        payCount: innerAudioContext.duration
      });
    });
    // this.dianzhan(e);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    njzxw.initFun(this);
    this.loadDatas(this);
  },
  loadDatas: function ($this) {
    njzxw.loadStart(this);
    this.loadData(this);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  loadData: function ($this) {
    njzxw.loadData($this, url.resourcesInfoFront.queryshoucangPage, {
      pageSize: 20,
      res_type: 2,
      stype:5
    }, function (data) {
      for (var i = 0; i < data.length; i++) {
        data[i]['play'] = false;
      }
      return data;
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.hidePay();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadDatas(this);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData(this);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})